package com.icss.ah.shop.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.util.DateUtils;
import com.icss.ah.login.bean.UserInfoBean;

/**
 * 店铺日销售汇总
 * @author csadchen
 * @date 2017-07-27
 */
@Controller
public class ShopDateSellAction extends BaseAction{

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private String dateRange;
	private String analysisType;
	/**
	 * 进入日销售分析页面
	 * @return
	 */
	public String shopDateSellInfo(){
		
		response = this.getResponse();
		request = this.getRequest();
		try {
			//获取登录账号信息
			UserInfoBean userInfo = getUserInfo("");
			//昨日日期
			String date = DateUtils.getDayByNum(DateUtils.getCurrentDate("yyyy-MM-dd"),"yyyy-MM-dd",-1);
			//查询参数
			HashMap queryMap = new HashMap();
			queryMap.put("date",date);
			queryMap.put("shopId", userInfo.getShopId());
			//获取昨日销售汇总数据
			HashMap dateInfo = shopDateSellService.queryShopSellData(queryMap);
			
			request.setAttribute("dateInfo", dateInfo);
		}  catch (Exception e) {
			errlog.error("ShopDateSellAction|queryShopSellData|异常：",e);
		}
		return "shopDateSellInfo";
	}
	/**
	 * 获取折线图数据
	 */
	public void queryChartData() {
		response = this.getResponse();
		request = this.getRequest();
		StringBuffer sBuffer = new StringBuffer();
		try {
			//获取登录账号信息
			UserInfoBean userInfo = getUserInfo("");
			//折线图展示数据x维度
			dateRange = dateRange == null ? "7" : dateRange;
			//折线图展示数据y维度
			analysisType = analysisType == null ? "0" : analysisType;
			//根据dateRange获取x维度起始日期
			int dayNum = Integer.valueOf(dateRange);
			String date = DateUtils.getDayByNum(DateUtils.getCurrentDate("yyyy-MM-dd"),"yyyy-MM-dd",0-dayNum);
			//参数
			HashMap queryMap = new HashMap();
			queryMap.put("date",date);
			queryMap.put("shopId", userInfo.getShopId());
			
			List<Map> list = new ArrayList();
			
			//查询折线图表数据
			list = shopDateSellService.queryChartData(queryMap);
			//这些图图表数据处理并返回sBuffer
			shopDateSellService.setNum(analysisType, sBuffer, list);
			
		} catch (Exception e) {
			errlog.error("ShopDateSellAction|queryChartData|异常：",e);
		} finally{
			//返回图表数据
			str2Resp(sBuffer.toString());
		}
	}
	/**
	 * 获取表格数据
	 */
	public void queryTableData() {
		response = this.getResponse();
		request = this.getRequest();
		String result = "";
		try {
			//获取登录账号信息
			UserInfoBean userInfo = getUserInfo("");
			//折线图展示数据x维度
			dateRange = dateRange == null ? "7" : dateRange;
			//折线图展示数据y维度
			analysisType = analysisType == null ? "0" : analysisType;
			//根据dateRange获取x维度起始日期
			int dayNum = Integer.valueOf(dateRange);
			String date = DateUtils.getDayByNum(DateUtils.getCurrentDate("yyyy-MM-dd"),"yyyy-MM-dd",0-dayNum);
			//参数
			HashMap queryMap = new HashMap();
			queryMap.put("date",date);
			queryMap.put("orderBy"," DESC ");
			queryMap.put("shopId", userInfo.getShopId());
			
			List<Map> list = new ArrayList();
			//查询表格数据
			list = shopDateSellService.queryChartData(queryMap);
			//返回表格数据
			result = JSONArray.fromObject(list).toString();
		} catch (Exception e) {
			errlog.error("ShopDateSellAction|queryTableData|异常：",e);
		} finally{
			str2Resp(result);
		}
	}

	public String getDateRange() {
		return dateRange;
	}

	public void setDateRange(String dateRange) {
		this.dateRange = dateRange;
	}

	public String getAnalysisType() {
		return analysisType;
	}

	public void setAnalysisType(String analysisType) {
		this.analysisType = analysisType;
	}
}
